import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:opene/JhPickerTool.dart';
import 'package:opene/app_constant.dart';
import 'package:opene/app_theme.dart';
import 'package:opene/app_utils.dart';
import 'package:opene/base/base_state.dart';
import 'package:opene/base/loading.dart';
import 'package:opene/net/method/base_method.dart';
import 'package:opene/net/network_manager.dart';
import 'package:opene/net/response/base_response.dart';
import 'package:opene/ui/cms/cmsmodel.dart';
import 'package:toast/toast.dart';

class CMSCheckinEmptyScreen extends StatelessWidget {
  CMSCheckin checkinInfo;

  CMSCheckinEmptyScreen({this.checkinInfo});

  @override
  Widget build(BuildContext context) {
    return CMSCheckinEmptyPage(checkinInfo: checkinInfo);
  }
}

class CMSCheckinEmptyPage extends StatefulWidget {
  CMSCheckin checkinInfo;

  CMSCheckinEmptyPage({this.checkinInfo});

  @override
  _CMSCheckinEmptyPageState createState() => _CMSCheckinEmptyPageState(checkinInfo: checkinInfo);
}

class _CMSCheckinEmptyPageState extends BaseState<CMSCheckinEmptyScreen> {
  CMSCheckin checkinInfo;

  _CMSCheckinEmptyPageState({this.checkinInfo});

  final TextEditingController dateController = TextEditingController();

  String strDate;

  @override
  String setTitle({String title}) {
    return !Utils.isEmpty(title) ? title : "Empty";
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setResetGone();
    setState(() {
      next = "Submit";
    });
  }

  @override
  Widget getViewBinding() {
    return Padding(
      padding: const EdgeInsets.only(left: 0, top: 10, right: 0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(left: 15),
            child: Row(
              children: [
                Text(
                  "Container NO#: ",
                  style: TextStyle(color: MyAppTheme.color_666666, fontSize: 14.0),
                ),
                Text(
                  Utils.getStringisEmpty(checkinInfo.containerNo),
                  style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                ),
              ],
            ),
          ),
          Container(
            height: 40,
            margin: const EdgeInsets.only(left: 15, top: 3, right: 15, bottom: 0),
            width: double.infinity,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Text(
                  "Empty Date: ",
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(color: MyAppTheme.color_666666, fontSize: 12.0),
                ),
                Expanded(
                  flex: 1, //设置权重
                  child: GestureDetector(
                    onTap: () {
                      // 请求焦点
                      FocusScope.of(context).requestFocus(FocusNode());

                      JhPickerTool.showDatePicker(context, clickCallback: (var timeStr, var str) {
                        print(timeStr);
                        print(str);
                        setState(() {
                          strDate = timeStr;
                        });
                      });
                    },
                    child: Container(
                      height: 35,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(20.0)),
                        color: MyAppTheme.white,
                        border: Border.all(
                          color: MyAppTheme.nearlyBlue,
                          width: 1.0,
                        ),
                      ),
                      child: Row(
                        children: [
                          Container(
                            margin: const EdgeInsets.only(left: 0, top: 0, right: 9, bottom: 0),
                            width: 1,
                          ),
                          Expanded(
                            flex: 1, //设置权重
                            child: Text(
                              Utils.getStringisEmpty(strDate),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(color: MyAppTheme.color_333333),
                            ),
                          ),
                          Icon(
                            Icons.date_range,
                            size: 25,
                            color: MyAppTheme.nearlyBlue,
                          ),
                          Container(
                            margin: const EdgeInsets.only(left: 0, top: 0, right: 9, bottom: 0),
                            width: 1,
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  void showFileUploadData(String path, int type, int position, bool isSubmit) {}

  @override
  void setNext() {
    ToastContext().init(context);
    if (Utils.isEmpty(strDate)) {
      Toast.show("Please select Empty Date", duration: Toast.lengthShort, gravity: Toast.bottom);
      return;
    }
    checkSubmit();
  }

  @override
  void setReset() {
    setState(() {
      textEditingController.text = "";
      strDate = "";
    });
  }

  void checkSubmit() async {
    CMSCheckinEmpty checkinPendingQr = new CMSCheckinEmpty();
    checkinPendingQr.containerNo = checkinInfo.containerNo;
    checkinPendingQr.unloadDate = strDate;
    String json = jsonEncode(checkinPendingQr);
    try {
      // 显示加载中对话框
      LoadingIndicator.show(context);
    } catch (e) {
      e.toString();
    }
    BaseResponse baseResponse = await NetworkManager().request(context, Constants.fbaTransNotifyUnloadUnloadCommit, method: BaseMethod.post, data: json);

    try {
      // 在需要的时候隐藏加载中对话框
      LoadingIndicator.hide();
    } catch (e) {
      e.toString();
    }

    if (baseResponse.status == 200) {
      Navigator.pop(context, true);
    } else {
      ToastContext().init(context);
      Toast.show(Utils.getStringisEmpty(baseResponse.msg), duration: Toast.lengthShort, gravity: Toast.bottom);
    }
  }
}
